﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using NetBay.Core.Log;
using Rio.Framework.Historical;
using System.Data;
#endregion

namespace Rio.Framework.Business.Repository.Translator
{
    public class SettingsTranslator
    {
        /// <summary>
        /// Read a row from DataReader and convert it to a Setting object
        /// </summary>
        /// <param name="p_oIDataReader">DataReader to read</param>
        /// <returns>Setting object</returns>
        public static Setting cTranslateSetting(IDataReader p_oIDataReader)
        {
            var cRet = new Setting();
            cRet.IsValid = false;

            try
            {
                // Something to read?
                if (p_oIDataReader.Read())
                {
                    // Reads values from DataReader and translate it into a correct value
                    //cRet.iId = (int)SettingsTranslate.Translate(p_oDbDataReader[Setting.Column.Id.szGetDescription()].ToString());
                    cRet.szName = p_oIDataReader[Setting.Column.Name.szGetDescription()].ToString();
                    cRet.szDescription = p_oIDataReader[Setting.Column.Description.szGetDescription()].ToString();
                    cRet.szValue =
                        SettingsTable.Translate(
                            p_oIDataReader[Setting.Column.Domain.szGetDescription()].ToString(),
                            p_oIDataReader[Setting.Column.Name.szGetDescription()].ToString(),
                            p_oIDataReader[Setting.Column.Value.szGetDescription()].ToString());
                    cRet.szDomain = p_oIDataReader[Setting.Column.Domain.szGetDescription()].ToString();
                    // Read and translate correctly
                    cRet.IsValid = true;
                }
                else
                    // Inform that it's no more data to read
                    cRet.ErrorCode = (int)ReturnObject.ErrorCodes.NoData;
            }
            catch (Exception ex)
            {
                cRet.ErrorCode = (int)ReturnObject.ErrorCodes.UnknowError;
                LogManager.Instance().LogError("EXCEPTION -> " + Environment.NewLine + ex);
            }
            return cRet;
        }
    }
}
